﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace Registrar
{
    public partial class StudentRegistry : Form
    {
      public List<Student> students = new List<Student>();  
      
      public StudentRegistry()
      {
        InitializeComponent();
        ReadAllStudentsFromDisk();
        studentBindingSource.DataSource = students;
      }

      public void SaveAllStudents()
      {
          XmlWriter writer = new XmlTextWriter("Students.xml", Encoding.ASCII);
          XmlSerializer x = new XmlSerializer( students.GetType() );
          x.Serialize(writer, students);
          writer.Close();
      }
        
      public void ReadAllStudentsFromDisk()
      {
          XmlReader reader = new XmlTextReader("Students.xml");
          XmlSerializer x = new XmlSerializer(students.GetType());
          students = x.Deserialize(reader) as List<Student>;
          reader.Close();
      }

      private void saveToolStripMenuItem_Click(object sender, EventArgs e)
      {
          SaveAllStudents();
      }

      private void exitToolStripMenuItem_Click(object sender, EventArgs e)
      {
          SaveAllStudents();
          this.Close();
      }

    }
}
